using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace AccesoDatos
{
    public class ConexionBD
    {
        private static ConexionBD instancia;
        private string sConnectionString;
        private SqlConnection connection;
        private SqlTransaction transaction;
        private bool enTransaccion;

        public static ConexionBD ObtenerInstancia()
        {
            if (instancia == null)
            {
                instancia = new ConexionBD(); 
            }
            return instancia;
        }

        //Constructor de la clase
        private ConexionBD()
        {
            sConnectionString = "Data source=.\\SQLEXPRESS;Initial Catalog=RentasBD3;integrated security=true";
            try
            {
                this.connection = new SqlConnection(sConnectionString);
            }
            catch(SqlException ex){
            }
        }

        //Obtener el objeto conexion
        public SqlConnection ObtenerConexion()
        {
            //if (connection == null)
            //{
            //    this.connection = new SqlConnection(sConnectionString);
            //    this.connection.Open();
            //}
            //if (!this.enTransaccion)
                return this.connection;
            //else
            //    return this.transaction.Connection;
        }

        //Obtener la transaccion
        public SqlTransaction ObtenerTransaccion()
        {
            return this.transaction;
        }

        public void BeginTransaction(System.Data.IsolationLevel isolationLevel)
        {
            try
            {
                if (this.transaction == null)
                {
                    this.connection = this.ObtenerConexion();
                    this.enTransaccion = true;
                    this.transaction = connection.BeginTransaction(isolationLevel);
                }
            }
            catch (SqlException ex)
            {
            }
            
        }

        public void Commit()
        {

            try
            {
                if (this.connection != null && this.transaction != null)
                    this.transaction.Commit();
            }
            catch (SqlException ex) 
            {
            }
            finally
            {
                this.enTransaccion = false;
            }

        }


        public void Rollback()
        {

            try
            {
                if (this.connection != null && this.transaction != null)
                    this.transaction.Rollback();
            }
            catch (SqlException ex)
            {}   
            finally
            {    this.enTransaccion = false;
            
            }
        }

        //Se abre la conexion
        public void AbrirConexion()
        {
            if (this.connection.State != System.Data.ConnectionState.Open)
                this.connection.Open();
        }


        //Se finaliza la conexion
        public void CerrarConexion()
        {
            if (this.connection != null)
            {
                if (this.connection.State != System.Data.ConnectionState.Closed)
                {
                    this.connection.Close();
                    //this.connection.Dispose();
                    //this.connection = null;
                }
            }
        }
    }
}
